#include "categoryselectdialog.h"
#include "ui_categoryselectdialog.h"

namespace acidone
{

CategorySelectDialog::CategorySelectDialog(Database::Categories categories, Database::Categories selectedCategories, QWidget *parent)
	:QDialog(parent)
	,ui(new Ui::CategorySelectDialog)
{
    ui->setupUi(this);

	foreach(Category* category, categories)
	{
		QListWidgetItem* item = new QListWidgetItem(category->name());
		item->setFlags(item->flags()|Qt::ItemIsUserCheckable);
		item->setData(Qt::UserRole, qVariantFromValue(category));
		item->setCheckState(selectedCategories.contains(category) ? Qt::Checked : Qt::Unchecked);
		ui->listWidget->addItem(item);
	}
}

void CategorySelectDialog::accept()
{
	for(int i = 0; i < ui->listWidget->count(); ++i)
	{
		QListWidgetItem* item = ui->listWidget->item(i);
		if (item->checkState() == Qt::Checked)
		{
			m_selectedCategories.append(item->data(Qt::UserRole).value<Category*>());
		}
	}
	QDialog::accept();
}

Database::Categories CategorySelectDialog::selectedCategories() const
{
	return m_selectedCategories;
}

CategorySelectDialog::~CategorySelectDialog()
{
    delete ui;
}

}
